package com.rongshu.view.penpal.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.mvp.INetView;
import com.rongshu.view.penpal.bean.PenpalNoticeResponse;
import com.rongshu.view.penpal.present.AddPenpalPresenter;
import com.widget.titlebar.ImmersionBar;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * created by zhengweis on 2018/11/26.
 * description：
 */
public class PenpalEditNoticeActivity extends BaseActivity<AddPenpalPresenter> implements INetView {
    @BindView(R.id.head_middle)
    TextView headMiddle;
    @BindView(R.id.head_content)
    RelativeLayout headContent;
    @BindView(R.id.content_length)
    TextView contentLength;
    @BindView(R.id.content)
    EditText content;
    @BindView(R.id.head_right)
    TextView headRight;
    private int penpalwill_id;
    private PenpalNoticeResponse.ResponseBean bean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImmersionBar bar = ImmersionBar.with(this);
        bar.reset().keyboardEnable(true, WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE
                | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        if (ImmersionBar.isSupportStatusBarDarkFont()) {//是否支持修改状态栏字体颜色
            bar.transparentStatusBar()  //透明状态栏，不写默认透明色
                    .statusBarDarkFont(true)   //状态栏字体是深色，不写默认为亮色
                    .flymeOSStatusBarFontColor(R.color.color_2c2c2c)  //修改flyme OS状态栏字体颜色
                    .statusBarAlpha(0);
        } else {
            bar.transparentStatusBar()  //透明状态栏，不写默认透明色
                    .flymeOSStatusBarFontColor(R.color.black)  //修改flyme OS状态栏字体颜色
                    //原理：如果当前设备支持状态栏字体变色，会设置状态栏字体为黑色，如果当前设备不支持状态栏字体变色，会使当前状态栏加上透明度，否则不执行透明度
                    .statusBarDarkFont(true, 1.0f);
        }
        bar.init();
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_add_penpal_notice;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        if (intent != null) {
            penpalwill_id = intent.getIntExtra(Constant.PENPAL_ID, 0);
            bean = (PenpalNoticeResponse.ResponseBean) intent.getSerializableExtra(Constant.PENPAL_NOTICE);
            if (bean != null) {
                try {
                    content.setText(URLDecoder.decode(bean.getContents(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                contentLength.setText(bean.getContents().length() + "/500");
            }
        }
        headMiddle.setText("编辑内容");
        headRight.setText("发布");
        headRight.setTextColor(ContextCompat.getColor(this, R.color.color_3CC051));
    }

    @Override
    protected void initData() {
        setPresenter(new AddPenpalPresenter(this), this);
        content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String msg = presenter.replaceBlank(editable.toString());
                contentLength.setText(msg.length() + "/500");
            }
        });
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @OnClick({R.id.head_left, R.id.head_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.head_left:
                showKeyboard(false);
                finish();
                break;
            case R.id.head_right:
                if (TextUtils.isEmpty(content.getText().toString())) {
                    toast("您还未输入内容");
                } else {
                    showKeyboard(false);
                    if (bean != null) {
                        presenter.editNotice(bean.getId(), content.getText().toString());
                    } else {
                        presenter.addNotice(penpalwill_id, content.getText().toString());
                    }
                }
                break;
        }
    }

    @Override
    public void success(Object object, int tag) {
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void faild(Object object, int tag) {

    }
}
